package com.ease.framework.gef4g.actions {
   import com.ease.framework.gef.events.GraphicalZoomEvent;
   import com.ease.framework.gef4g.IGraphicalEditor;
   import com.ease.framework.workbench.Workbench;

   import flash.events.MouseEvent;

   import mx.containers.Box;
   import mx.containers.BoxDirection;
   import mx.containers.HBox;
   import mx.controls.Button;
   import mx.controls.Spacer;
   import mx.controls.VSlider;
   import mx.core.Application;
   import mx.core.FlexGlobals;
   import mx.events.SliderEvent;

   public class ZoomSlider extends Box {
      private var slider:VSlider;

      public function ZoomSlider() {
         super();
         this.direction = BoxDirection.VERTICAL;
         this.width = 20;
         setStyle("verticalGap", 1);

         addChild(new Spacer());

         var btn_zoomin:Button = new Button();
         btn_zoomin.styleName = "ZoomInButton";
         btn_zoomin.addEventListener(MouseEvent.CLICK, onMouseClick_in);
         addChild(btn_zoomin);

         var tmpSBox:HBox = new HBox();
         tmpSBox.width = 20;

         slider = new VSlider();
         slider.minimum = 0.25;
         slider.maximum = 3.00;
         slider.value = 1;
         slider.snapInterval = 0.01;
         slider.liveDragging = true;
         slider.addEventListener(SliderEvent.CHANGE, onSliderChange);

         tmpSBox.addChild(slider);

         addChild(tmpSBox);

         var btn_zoomout:Button = new Button();
         btn_zoomout.styleName = "ZoomOutButton";
         btn_zoomout.addEventListener(MouseEvent.CLICK, onMouseClick_out);
         addChild(btn_zoomout);

         FlexGlobals.topLevelApplication.addEventListener(GraphicalZoomEvent.ZOOM_CHANGED, onZoomChanged);
      }

      protected function onMouseClick_in(event:MouseEvent):void {
         slider.value = slider.value + slider.snapInterval;
         slider.dispatchEvent(new SliderEvent(SliderEvent.CHANGE));
      }

      protected function onMouseClick_out(event:MouseEvent):void {
         slider.value = slider.value - slider.snapInterval;
         slider.dispatchEvent(new SliderEvent(SliderEvent.CHANGE));
      }

      protected function onSliderChange(event:SliderEvent):void {
         var zoomNum:Number = event.target.value;
         IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().doZoom(zoomNum);
      }

      protected function onZoomChanged(event:GraphicalZoomEvent):void {
         slider.value = IGraphicalEditor(Workbench.instance.currentEditor).getGraphicalViewer().getZoomCurrentNumber();
      }
   }
}